package com.ccdc.lbcmsystem.controller;

import com.ccdc.lbcmsystem.mapper.FeedBackMapper;
import com.ccdc.lbcmsystem.mapper.NoticeMapper;
import com.ccdc.lbcmsystem.mapper.UserMapper;
import com.ccdc.lbcmsystem.pojo.FeedBack;
import com.ccdc.lbcmsystem.pojo.Notice;
import com.ccdc.lbcmsystem.pojo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Controller
public class SystemController {
    @Autowired
    FeedBackMapper feedBackMapper;
    @Autowired
    JavaMailSenderImpl mailSender;
    @Autowired
    UserMapper userMapper;
    @Autowired
    NoticeMapper noticeMapper;
    //系统公告管理
    @RequestMapping("/notice")
    public String notice(Model model,@RequestParam(defaultValue = "1") Integer startPage,
                         @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<Notice> notices = noticeMapper.queryList();
        PageInfo<Notice> noticesPageInfo = new PageInfo<>(notices);
        model.addAttribute("noticesPageInfo", noticesPageInfo);
        model.addAttribute("notices",notices);
        return "system/notice";
    }

    @RequestMapping("/searchNotice")
    public String searchNotice(Model model,@RequestParam(defaultValue = "1") Integer startPage,
                         @RequestParam(defaultValue = "10") Integer pageSize,String theme, HttpSession session){
        PageHelper.startPage(startPage, pageSize);
        List<Notice> notices = noticeMapper.queryListByTheme(theme);
        PageInfo<Notice> noticesPageInfo = new PageInfo<>(notices);
        session.setAttribute("theme",theme);
        model.addAttribute("noticesPageInfo", noticesPageInfo);
        model.addAttribute("notices",notices);
        return "system/noticeSearch";
    }

    @RequestMapping("/noticeLookInfo/{id}")
    public String noticeLookInfo(@PathVariable("id") Integer id, Model model){
        Notice notice = noticeMapper.queryNoticeById(id);
        model.addAttribute("notice",notice);
        return "system/noticeLookInfo";
    }

    @RequestMapping("/toNoticeAdd")
    public String toNoticeAdd(){
        return "system/noticeAdd";
    }
    @RequestMapping("/noticeAdd")
    @ResponseBody
    public String noticeAdd(Notice notice) {
        String msg = "";
        SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime = new Date();
        String time = sft.format(createTime);
        notice.setCreateTime(time);
        notice.setStatus(1);
        try {
            noticeMapper.addNotice(notice);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "新增失败";
            return msg;
        }

    }
    @RequestMapping("/delNotice")
    @ResponseBody
    public String delNotice(Integer id){
        String msg = "";
        try {
            noticeMapper.delNotice(id);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "删除失败";
            return msg;
        }
    }
    @RequestMapping("/toNoticeEdit/{id}")
    public String toNoticeEdit(@PathVariable("id") Integer id, Model model){
        Notice notice = noticeMapper.queryNoticeById(id);
        model.addAttribute("notice",notice);
        return "system/noticeEdit";
    }
    @RequestMapping("/editNotice")
    @ResponseBody
    public String editNotice(Integer id,String theme,String content){
        String msg = "";
        try {
            noticeMapper.editNoticeById(id,theme,content);
            msg = "OK";
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg = "修改失败";
            return msg;
        }
    }
    //上架
    @RequestMapping("/upNotice")
    @ResponseBody
    public String upNotice(Integer id){
        String msg = "";
        try {
            noticeMapper.editNotice();
            Integer status = 0;
            noticeMapper.editNoticeStatusById(id,status);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "操作失败";
            return msg;
        }
    }
    //下架
    @RequestMapping("/downNotice")
    @ResponseBody
    public String downNotice(Integer id){
        String msg = "";
        try {
            Integer status = 1;
            noticeMapper.editNoticeStatusById(id,status);
            msg = "OK";
            return msg;
        }catch (Exception e){
            msg = "操作失败";
            return msg;
        }
    }
    //信息反馈管理
    @RequestMapping("/feedBackManage")
    public String feedBackManage(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                 @RequestParam(defaultValue = "10") Integer pageSize){
        PageHelper.startPage(startPage, pageSize);
        List<FeedBack> feedBacks = feedBackMapper.queryList();
        PageInfo<FeedBack> feedBacksPageInfo = new PageInfo<>(feedBacks);
        model.addAttribute("feedBacksPageInfo", feedBacksPageInfo);
        model.addAttribute("feedBacks",feedBacks);
        return "system/feedBackManage";
    }

    @RequestMapping("/feedBackLookInfo/{id}")
    public String feedBackLookInfo(@PathVariable("id") Integer id, Model model){
        FeedBack feedBack = feedBackMapper.queryFeedBackById(id);
        model.addAttribute("feedBack",feedBack);
        return "system/feedBackManageLookInfo";
    }

    @RequestMapping("/redFeedBack")
    @ResponseBody
    public String redFeedBack(Integer id){
        String msg = "";
        FeedBack feedBack = feedBackMapper.queryFeedBackById(id);
        if (feedBack.getStatus().equals("1")){
            msg = "该信息已阅，请勿重复操作！";
            return msg;
        }
        else {
            try {
                String status = "1";
                feedBackMapper.updateFeedBack(id,status);
                msg = "OK";
                return msg;
            }catch (Exception e){
                e.printStackTrace();
                msg = "操作失败";
                return msg;
            }
        }

    }
    //采纳
    @RequestMapping("/adoptFeedBack")
    @ResponseBody
    public String adoptFeedBack(Integer id){
        String msg = "";
        FeedBack feedBack = feedBackMapper.queryFeedBackById(id);
        if (feedBack.getStatus().equals("2") ){
            msg = "该信息已采纳，请勿重复操作！";
            return msg;
        }
        else {
            try {
                String status = "2";
                feedBackMapper.updateFeedBack(id,status);
                MimeMessage mimeMessage = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
                helper.setSubject("系统通知");
                helper.setText("您好！您反馈的信息管理员已采纳，感谢您对我们提供的宝贵意见！",true);
                String from = "1245996419@qq.com";
                String nickName = "留守儿童信息管理系统";
                //根据id查询出信息
                User user = userMapper.queryUserByUserName(feedBack.getName());
                helper.setTo(user.getEmail());
                helper.setFrom(nickName+'<'+from+'>');
                mailSender.send(mimeMessage);
                msg = "OK";
                return msg;
            }catch (Exception e){
                e.printStackTrace();
                msg = "操作失败";
                return msg;
            }
        }

    }

    @RequestMapping("/searchFeedBack")
    public String searchFeedBack(Model model, @RequestParam(defaultValue = "1") Integer startPage,
                                 @RequestParam(defaultValue = "10") Integer pageSize, String name,
                                 HttpSession session,String theme,String status){
        PageHelper.startPage(startPage, pageSize);
        List<FeedBack> feedBacks = feedBackMapper.searchFeedBack(name, status, theme);
        PageInfo<FeedBack> feedBacksPageInfo = new PageInfo<>(feedBacks);
        session.setAttribute("name",name);
        session.setAttribute("status",status);
        session.setAttribute("theme",theme);
        model.addAttribute("feedBacksPageInfo", feedBacksPageInfo);
        model.addAttribute("feedBacks",feedBacks);
        return "system/feedBackManageSearch";
    }
}
